package cn.iocoder.yudao.module.datasource.dal.mysql.config;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.datasource.controller.admin.config.vo.DataSourceConfigPageReqVO;
import cn.iocoder.yudao.module.datasource.dal.dataobject.config.DynamicDataSourceConfigDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * 数据源配置 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface DataSourceConfigMapper extends BaseMapperX<DynamicDataSourceConfigDO> {

    default PageResult<DynamicDataSourceConfigDO> selectPage(DataSourceConfigPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<DynamicDataSourceConfigDO>()
                .likeIfPresent(DynamicDataSourceConfigDO::getName, reqVO.getName())
                .eqIfPresent(DynamicDataSourceConfigDO::getType, reqVO.getType())
                .eqIfPresent(DynamicDataSourceConfigDO::getEnabled, reqVO.getEnabled())
                .orderByAsc(DynamicDataSourceConfigDO::getSort).orderByDesc(DynamicDataSourceConfigDO::getId));
    }

    default List<DynamicDataSourceConfigDO> selectListByEnabled(Boolean enabled) {
        return selectList(new LambdaQueryWrapperX<DynamicDataSourceConfigDO>()
                .eqIfPresent(DynamicDataSourceConfigDO::getEnabled, enabled)
                .orderByAsc(DynamicDataSourceConfigDO::getSort).orderByDesc(DynamicDataSourceConfigDO::getId));
    }

    default List<DynamicDataSourceConfigDO> selectList(DataSourceConfigPageReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<DynamicDataSourceConfigDO>()
                .likeIfPresent(DynamicDataSourceConfigDO::getName, reqVO.getName())
                .eqIfPresent(DynamicDataSourceConfigDO::getType, reqVO.getType())
                .eqIfPresent(DynamicDataSourceConfigDO::getEnabled, reqVO.getEnabled())
                .orderByAsc(DynamicDataSourceConfigDO::getSort).orderByDesc(DynamicDataSourceConfigDO::getId));
    }

    default DynamicDataSourceConfigDO selectByName(String name) {
        return selectOne(DynamicDataSourceConfigDO::getName, name);
    }

}